
function ensenOnchange(objPref, objRailCd) {
	objRailCd.onchange = function(event){
		
		$.ajax({
			beforeSend: function(xhr){
		    	 xhr.overrideMimeType("text/html;charset=UTF-8");
			},
			type: "POST",
			url: "/sp/search/requestsearch/eki/",
			data: {PrefCd:objPref.value,RailCd:objRailCd.value},
			success: function(result1){
				
				$("#eki_block1").html('');
				$("#eki_block1").html('');
				$("#eki_block1").html('');
				
				$("#eki_block1").html(result1);
				$("#eki_block2").html(result1.replace(/StCd1/g, 'StCd2'));
				$("#eki_block3").html(result1.replace(/StCd1/g, 'StCd3'));
					
				$("#eki_block1 select").customSelect({customClass: "custom_select"});
				$("#eki_block2 select").customSelect({customClass: "custom_select"});
				$("#eki_block3 select").customSelect({customClass: "custom_select"});
			}
		});
	};
}
						

$(function(){
	
	/*******************************************************
	 * エリア選択
	 *******************************************************/
	
	/*******************************************************
	 * エリア選択
	 *******************************************************/
	function conditionArea(){
		var $list = $('#sel_area');
	
		$list.find('input').each(function(i){
			$(this).on('click', function(e){
				changeSelect(i);
			});
		});
	
		function changeSelect(n){
			if (n == 0) {
				$("#li_api_block").show();
				$("#li_ensen_block").hide();
				$("#li_eki_block1").hide();
				$("#li_eki_block2").hide();
				$("#li_eki_block3").hide();
				$("#select_mode").attr("class", "area");
				
			} else {
				$("#li_api_block").hide();
				$("#li_ensen_block").show();
				$("#li_eki_block1").show();
				$("#li_eki_block2").show();
				$("#li_eki_block3").show();
				$("#select_mode").attr("class", "line");
			};
			
			if($("#hidArea").val() != n)
			{
				$("#PrefCd option:first").prop('selected','selected');
				$("#pref_block").find('.custom_selectInner').text($("#PrefCd option:first").text());
				$("#PrefCd").trigger("onchange");
				$("#hidArea").val(-1);
			}
			
		};

		$list.find('input').each(function(i){
			if ($(this).prop('checked')) {
				changeSelect(i);
				return false;
			};
		});
	};

	conditionArea();
	
	
	var objPref = document.getElementById("PrefCd");
	var objRailCd = document.getElementById("RailCd");
	
	if(!!objRailCd)
		ensenOnchange(objPref, objRailCd);
	
	if (!!objPref) {
		
		objPref.onchange = function(event){
			
			var area = jQuery( 'input[name=area]:checked' ).val();
			if(area == "0")
			{
				$.ajax({
					beforeSend: function(xhr){
				    	 xhr.overrideMimeType("text/html;charset=UTF-8");
					},
					type: "POST",
					url: "/sp/search/requestsearch/city/",
					data: {PrefCd:objPref.value},
					success: function(result){
						
						$("#api_block").html('');
						$("#api_block").html(result);
						//$('#api_block select').customSelect({customClass: 'custom_select'});
					}
				});
			}
			else
			{
				$.ajax({
					beforeSend: function(xhr){
				    	 xhr.overrideMimeType("text/html;charset=UTF-8");
					},
					type: "POST",
					url: "/sp/search/requestsearch/ensen/",
					data: {PrefCd:objPref.value},
					success: function(result){
						$("#ensen_block").html(result);
						//$("#ensen_block select").customSelect({customClass: "custom_select"});
						
						var objRailCd = document.getElementById("RailCd");
						ensenOnchange(objPref, objRailCd);
						
						$("#RailCd").trigger("onchange");
					}
				});
			}
				
		};
	}

	$("#usrform").validate({
    	rules: {
    		PrefCd: {
				required: true
			},
			CityCd: {
				required: { depends: function () {
										var area = jQuery( 'input[name=area]:checked' ).val();
										if(area == "1") return false;
					                    return true;
						   			}
					}
			},
			RailCd: {
				required: { depends: function () {
										var area = jQuery( 'input[name=area]:checked' ).val();
										if(area == "0") return false;
					                    return true;
						   			}
					}
			},
			StCd1: {
				required: { depends: function () {
										var area = jQuery( 'input[name=area]:checked' ).val();
										if(area == "0") return false;
										if( $("#StCd2").val() != "" || $("#StCd3").val() != "" ) return false;
					                    return true;
						   			}
					}
			},
			tinryo : {
				required: true
			},
			madori : {
				required: true
			}
		},
		messages: {
			PrefCd: {
				required: "<li>都道府県を選択してください</li>"
			},
			CityCd: {
				required: "<li>市区町村を選択してください</li>"
			},
			RailCd: {
				required: "<li>路線・駅を選択してください</li>"
			},
			StCd1: {
				required: "<li>路線・駅を選択してください</li>"
			},
			tinryo: {
				required: "<li>ご希望の家賃を選択してください</li>"
			},
			madori: {
				required: "<li>ご希望の間取りを選択してください</li>"
			}
		},
		errorPlacement: function (error, element) {
			var elmName = element.attr("name");
			
			if (elmName == "PrefCd" || elmName == "CityCd" || elmName == "RailCd"　|| elmName == "StCd1"　)
            {
				if($("#areaerr").html().indexOf(error[0].innerHTML) < 0 )
					$("#areaerr").append(error[0].innerHTML);
            }
			else if(elmName == "tinryo")
			{
				if($("#kibouerr").html().indexOf(error[0].innerHTML) < 0 )
					$("#kibouerr").append(error[0].innerHTML);
			}
			else if(elmName == "madori")
			{
				if($("#madorierr").html().indexOf(error[0].innerHTML) < 0 )
					$("#madorierr").append(error[0].innerHTML);
			}
		} ,
		onfocusout: false,
		onkeyup: false          
    });
    
	$("#btnNext").click(function (evt) {
		
		$("#areaerr").empty();
		$("#kibouerr").empty();
		$("#madorierr").empty();
		
    	if ($("#usrform").validate().form()) {
    		
    		var area = jQuery( 'input[name=area]:checked' ).val();
			
    		if( area == "0"){
    			$("#hidAreaDisplay").val($("#PrefCd option:selected").text() + "　" + $("#CityCd option:selected").text());
    		}
    		else {
    			var str = $("#RailCd option:selected").text();
    			str += $("#StCd1").val() != "" ? "　" + $("#StCd1 option:selected").text() : "";
				str += $("#StCd2").val() != "" ? "　" + $("#StCd2 option:selected").text() : "";
				str += $("#StCd3").val() != "" ? "　" + $("#StCd3 option:selected").text() : "";
				$("#hidAreaDisplay").val(str);
    		}
    			
    		$("#hidYachinDisplay").val($("#tinryo option:selected").text() + ($("#tinryo").val() < 9999999 ? "　以下" : ""));
    		$("#hidMadoriDisplay").val($("#madori option:selected").text() + ($("#madori").val() > 1 ? "　以上" : ""));
    		$("#hidMensekiDisplay").val($("#menseki").val() != "" ? $("#menseki option:selected").text() + ($("#menseki").val() > 1 ? "　以上" : "") : "");
    		$("#hidEkitohoDisplay").val($("#ekitoho").val() != "" ? $("#ekitoho option:selected").text() : "");
    		
    		document.usrform.action= "/sp/search/requestsearch/shop/";
    		document.usrform.submit();
        }
    	else
    	{
    		if($("#areaerr > *").length > 0){
    			$("#areaerr").show();
            	$("#areatitle").addClass("error");
            }
            else {
            	$("#areaerr").hide();
            	$("#areatitle").removeClass("error");
            }
    		
    		if($("#kibouerr > *").length > 0){
    			$("#kibouerr").show();
            	$("#kiboutitle").addClass("error");
            }
            else {
            	$("#kibouerr").hide();
            	$("#kiboutitle").removeClass("error");
            }
    		
    		if($("#madorierr > *").length > 0){
    			$("#madorierr").show();
            	$("#madorititle").addClass("error");
            }
            else {
            	$("#madorierr").hide();
            	$("#madorititle").removeClass("error");
            }
    		
    	}
    });
})


